

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>RetinaNet Exporter &mdash; Nyoka 4.2.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript">
          var DOCUMENTATION_OPTIONS = {
              URL_ROOT:'../',
              VERSION:'4.2.0',
              LANGUAGE:'None',
              COLLAPSE_INDEX:false,
              FILE_SUFFIX:'.html',
              HAS_SOURCE:  true,
              SOURCELINK_SUFFIX: '.txt'
          };
      </script>
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home"> Nyoka
          

          
          </a>

          
            
            
              <div class="version">
                4.2
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
                <li class="toctree-l1"><a class="reference internal" href="../statsmodels_to_pmml.html">Statsmodels Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../keras_model_to_pmml.html">Keras Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../retinanet.html">RetinaNet Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../lgb_to_pmml.html">LightGBM Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../pre_process.html">Pre-Processing Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../skl_to_pmml.html">Scikit-Learn Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../xgboost_to_pmml.html">XGBoost Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../exponential_smoothing.html">ExponentialSmoothing Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../preprocess_nyoka.html">Nyoka's Pre-Processing Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../enums.html">Enums Module</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Nyoka</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> &raquo;</li>
        
          <li><a href="index.html">Module code</a> &raquo;</li>
        
      <li>RetinaNet Exporter</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for RetinaNet Exporter</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">absolute_import</span>

<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="n">BASE_DIR</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">BASE_DIR</span><span class="p">)</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">formatwarning</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">msg</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span><span class="o">+</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span>

<span class="kn">import</span> <span class="nn">PMML44</span> <span class="k">as</span> <span class="nn">pml</span>

<span class="kn">import</span> <span class="nn">keras_model_to_pmml</span> <span class="k">as</span> <span class="nn">kerasAPI</span>

<div class="viewcode-block" id="RetinanetToPmml"><span class="k">class</span> <span class="nc">RetinanetToPmml</span><span class="p">:</span>
    
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Write a PMML file for RetinaNet model.</span>

<span class="sd">    Parameters</span>
<span class="sd">    -----------</span>
<span class="sd">    model : </span>
<span class="sd">        RetinaNet model object</span>
<span class="sd">    input_shape : tuple </span>
<span class="sd">        Expected shape of the images to be scored</span>
<span class="sd">    backbone_name : string</span>
<span class="sd">        Name of backbone used to build the model. Valid values are `[&#39;resnet&#39;, &#39;mobilenet&#39;, &#39;densenet&#39;, &#39;vgg&#39;]`</span>
<span class="sd">    input_format : string (optional. default=&#39;image&#39;)</span>
<span class="sd">        Input format to be used during inference with the PMML. Valid values are - </span>
<span class="sd">            &quot;image&quot; : Original image in png format</span>
<span class="sd">            &quot;encoded&quot; : Base64 encoded string of the image</span>
<span class="sd">    trained_classes : list or tuple</span>
<span class="sd">        List of the classes on which the model was trained. If not provided, default(1 to 80) classes will be used</span>
<span class="sd">    pmml_file_name : string (default=&#39;from_retinanet.pmml&#39;)</span>
<span class="sd">        Name of the PMML file</span>
<span class="sd">    script_args : Dictionary or None</span>
<span class="sd">        Contains information of the script to be used to convert `image` data into base64 string. Required when dataSet=`image`.</span>
<span class="sd">        Required attributes - </span>
<span class="sd">            content : string or function</span>
<span class="sd">                The content of the script</span>
<span class="sd">            def_name : string</span>
<span class="sd">                name of the function to be used. Required when content is string</span>
<span class="sd">            return_type : string</span>
<span class="sd">                The return type of the function. Valid values are (&#39;string&#39;, &#39;double&#39;, &#39;float&#39;,&#39;integer&#39;)</span>
<span class="sd">            encode : boolean</span>
<span class="sd">                The representation of the script in PMML. If True, the script will be represented as base64 encoded string, else as plain text.</span>
<span class="sd">                If not provided, default value `True` is considered.</span>
<span class="sd">    model_name : string (optional)</span>
<span class="sd">        Name of the model</span>
<span class="sd">    description : string (optional)</span>
<span class="sd">        Description of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Creates Nyoka&#39;s PMML object and exports it to `pmml_file_name`</span>
<span class="sd">    </span>
<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="RetinanetToPmml.inference_error">    <span class="k">def</span> <span class="nf">inference_error</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Given model is not an inference model!&quot;</span></div>

<div class="viewcode-block" id="RetinanetToPmml.input_format_error">    <span class="k">def</span> <span class="nf">input_format_error</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Invalid input_format type. Valid values are `[&#39;image&#39;, &#39;encoded&#39;]`&quot;</span></div>

<div class="viewcode-block" id="RetinanetToPmml.backbone_name_error">    <span class="k">def</span> <span class="nf">backbone_name_error</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Invalid backbone_name. Valid values are `[&#39;resnet&#39;, &#39;mobilenet&#39;, &#39;densenet&#39;, &#39;vgg&#39;]`&quot;</span></div>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">input_shape</span><span class="p">,</span> <span class="n">backbone_name</span><span class="p">,</span> <span class="n">input_format</span><span class="o">=</span><span class="s2">&quot;image&quot;</span><span class="p">,</span> <span class="n">trained_classes</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
     <span class="n">pmml_file_name</span><span class="o">=</span><span class="s2">&quot;from_retinanet.pmml&quot;</span><span class="p">,</span> <span class="n">script_args</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">model_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">description</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">assert</span> <span class="n">model</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;FilterDetections&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">inference_error</span><span class="p">()</span>
        <span class="k">assert</span> <span class="n">input_format</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;image&#39;</span><span class="p">,</span><span class="s1">&#39;encoded&#39;</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">input_format_error</span><span class="p">()</span>
        <span class="k">assert</span> <span class="n">backbone_name</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;resnet&#39;</span><span class="p">,</span> <span class="s1">&#39;mobilenet&#39;</span><span class="p">,</span> <span class="s1">&#39;densenet&#39;</span><span class="p">,</span> <span class="s1">&#39;vgg&#39;</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">backbone_name_error</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">backbone_name</span> <span class="o">=</span> <span class="n">backbone_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">input_shape</span> <span class="o">=</span> <span class="n">input_shape</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">input_format</span> <span class="o">=</span> <span class="n">input_format</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">script_args</span> <span class="o">=</span> <span class="n">script_args</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span> <span class="o">=</span> <span class="n">model_name</span> <span class="k">if</span> <span class="n">model_name</span> <span class="k">else</span> <span class="s2">&quot;KerasRetinaNet&quot;</span><span class="o">+</span><span class="n">input_format</span><span class="o">.</span><span class="n">title</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="o">=</span> <span class="n">description</span> <span class="k">if</span> <span class="n">description</span> <span class="k">else</span> <span class="s2">&quot;RetinaNet model in PMML&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">pmml_obj</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_pyramid_layers</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;P3&quot;</span><span class="p">,</span> <span class="s2">&quot;P4&quot;</span><span class="p">,</span> <span class="s2">&quot;P5&quot;</span><span class="p">,</span> <span class="s2">&quot;P6&quot;</span><span class="p">,</span> <span class="s2">&quot;P7&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_layer_outputs</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">generate_pmml</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">input_shape</span><span class="p">,</span><span class="n">input_format</span><span class="p">,</span><span class="n">trained_classes</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pmml_obj</span><span class="o">.</span><span class="n">export</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="n">pmml_file_name</span><span class="p">,</span><span class="s1">&#39;w&#39;</span><span class="p">),</span><span class="mi">0</span><span class="p">)</span>


<div class="viewcode-block" id="RetinanetToPmml.generate_beckbone_anchors">    <span class="k">def</span> <span class="nf">generate_beckbone_anchors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">input_format</span><span class="p">,</span> <span class="n">trained_classes</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates PMML object for the backbone + anchors</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        model : </span>
<span class="sd">            RetinaNet model object</span>
<span class="sd">        input_format : string</span>
<span class="sd">            Input format to be used during inference with the PMML. Valid values are - </span>
<span class="sd">                &quot;image&quot; : Original image in png format</span>
<span class="sd">                &quot;encoded&quot; : Base64 encoded string of the image</span>
<span class="sd">        trained_classes : List</span>
<span class="sd">            List of class names for which the model was trained</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Nyoka&#39;s PMML object</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">keras.models</span> <span class="k">import</span> <span class="n">Sequential</span>
        <span class="n">mod</span> <span class="o">=</span> <span class="n">Sequential</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span>
            <span class="k">if</span> <span class="n">l</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;Model&quot;</span><span class="p">:</span>
                <span class="k">break</span>
            <span class="n">mod</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">trained_classes</span> <span class="o">==</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;trained_classes are not provided. Maximum 80 classes will be considered.&quot;</span><span class="p">)</span>
            <span class="n">trained_classes</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;Category_&quot;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">zfill</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">80</span><span class="p">)]</span>

        <span class="n">group1_pmml</span> <span class="o">=</span> <span class="n">kerasAPI</span><span class="o">.</span><span class="n">KerasToPmml</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span><span class="n">model_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="p">,</span><span class="n">dataSet</span><span class="o">=</span><span class="n">input_format</span><span class="p">,</span> <span class="n">description</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">description</span><span class="p">,</span>
         <span class="n">predictedClasses</span><span class="o">=</span><span class="n">trained_classes</span><span class="p">,</span> <span class="n">script_args</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">script_args</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">group1_pmml</span></div>

    
<div class="viewcode-block" id="RetinanetToPmml.generate_submodel">    <span class="k">def</span> <span class="nf">generate_submodel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">submodel</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates multiple PMML object for the regression and classification submodel of RetinaNet for each connected pyramid layers</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        submodel :</span>
<span class="sd">            The Regression or the Classification submodel</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        List of Nyoka&#39;s NetworkLayer object for all the submodels</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">net_layers_group</span><span class="o">=</span><span class="nb">list</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pyramid_layers</span><span class="p">):</span>
            <span class="n">nyoka_pmml_reg_mod</span> <span class="o">=</span> <span class="n">kerasAPI</span><span class="o">.</span><span class="n">KerasToPmml</span><span class="p">(</span><span class="n">submodel</span><span class="p">)</span>
            <span class="k">del</span> <span class="n">nyoka_pmml_reg_mod</span><span class="o">.</span><span class="n">DeepNetwork</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">NetworkLayer</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">nyoka_pmml_reg_mod</span><span class="o">.</span><span class="n">DeepNetwork</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">NetworkLayer</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">connectionLayerId</span> <span class="o">=</span> <span class="n">name</span>
            <span class="k">for</span> <span class="n">idx_</span><span class="p">,</span> <span class="n">lay</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">nyoka_pmml_reg_mod</span><span class="o">.</span><span class="n">DeepNetwork</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">NetworkLayer</span><span class="p">):</span>                
                <span class="n">lay</span><span class="o">.</span><span class="n">layerId</span> <span class="o">=</span> <span class="n">lay</span><span class="o">.</span><span class="n">layerId</span><span class="o">+</span><span class="s2">&quot;_&quot;</span><span class="o">+</span><span class="n">name</span>
                <span class="k">if</span> <span class="n">idx_</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">lay</span><span class="o">.</span><span class="n">connectionLayerId</span> <span class="o">=</span> <span class="n">lay</span><span class="o">.</span><span class="n">connectionLayerId</span><span class="o">+</span><span class="s2">&quot;_&quot;</span><span class="o">+</span><span class="n">name</span>
            <span class="n">net_layers_group</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">nyoka_pmml_reg_mod</span><span class="o">.</span><span class="n">DeepNetwork</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">NetworkLayer</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">net_layers_group</span></div>

    
<div class="viewcode-block" id="RetinanetToPmml.generate_inference_layers">    <span class="k">def</span> <span class="nf">generate_inference_layers</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates PMML object for the inference layers of RetinaNet</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        model :</span>
<span class="sd">            RetinaNet model object</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        List of Nyoka&#39;s NetworkLayer</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">inference_layers</span><span class="o">=</span> <span class="p">[</span><span class="n">lay</span> <span class="k">for</span> <span class="n">lay</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="o">-</span><span class="mi">8</span><span class="p">:]</span> <span class="k">if</span> <span class="n">lay</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">!=</span> <span class="s2">&quot;Model&quot;</span><span class="p">]</span>
        <span class="n">inference_network_layers</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">lay</span> <span class="ow">in</span> <span class="n">inference_layers</span><span class="p">:</span>
            <span class="n">connectLayerIds</span><span class="o">=</span><span class="nb">list</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">idx</span><span class="p">,</span><span class="n">lay_</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">lay</span><span class="o">.</span><span class="n">_inbound_nodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">inbound_layers</span><span class="p">):</span>
                <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">lay_</span><span class="p">,</span><span class="s1">&#39;layers&#39;</span><span class="p">):</span>
                    <span class="n">name</span> <span class="o">=</span> <span class="n">lay_</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">name</span><span class="o">+</span><span class="s2">&quot;_&quot;</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">_pyramid_layers</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">name</span> <span class="o">=</span> <span class="n">lay_</span><span class="o">.</span><span class="n">name</span>
                <span class="n">connectLayerIds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">lay</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;FilterDetections&#39;</span><span class="p">:</span>
                <span class="n">connectLayerIds</span> <span class="o">=</span> <span class="n">connectLayerIds</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span>
            <span class="n">network_layer</span><span class="o">=</span><span class="n">kerasAPI</span><span class="o">.</span><span class="n">KerasNetworkLayer</span><span class="p">(</span><span class="n">lay</span><span class="p">,</span><span class="s2">&quot;dataSet&quot;</span><span class="p">,</span><span class="n">lay</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">connectLayerIds</span><span class="p">)</span>
            <span class="n">network_layer</span><span class="o">.</span><span class="n">connectionLayerId</span> <span class="o">=</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">connectLayerIds</span><span class="p">)</span>
            <span class="n">inference_network_layers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">network_layer</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">inference_network_layers</span></div>

    
<div class="viewcode-block" id="RetinanetToPmml.assign_shapes">    <span class="k">def</span> <span class="nf">assign_shapes</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">input_shape</span><span class="p">,</span> <span class="n">pmml_without_shape</span><span class="p">):</span>
        <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Assigns the shape information to each NetworkLayer of the PMML</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        model :</span>
<span class="sd">            RetinaNet model object</span>
<span class="sd">        input_shape : tuple</span>
<span class="sd">            Input shape of each image used during training</span>
<span class="sd">        pmml_without_shape :</span>
<span class="sd">            Generated PMML object without shape information</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Nyoka&#39;s PMML object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">keras.models</span> <span class="k">import</span> <span class="n">Sequential</span>
        <span class="kn">from</span> <span class="nn">keras</span> <span class="k">import</span> <span class="n">backend</span> <span class="k">as</span> <span class="n">K</span>
        <span class="kn">from</span> <span class="nn">keras</span> <span class="k">import</span> <span class="n">Model</span>

        <span class="n">layer_output_dict</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>

        <span class="c1"># dummy data for shape calculation</span>
        <span class="n">sample_data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="n">input_shape</span><span class="p">)</span>
        <span class="n">nan_index</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">sample_data</span><span class="p">)</span>
        <span class="n">sample_data</span><span class="p">[</span><span class="n">nan_index</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.5</span>
        <span class="n">test</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">sample_data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        
        <span class="c1"># backbone and anchors</span>
        <span class="n">layers</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">layers</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">l</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;Model&quot;</span><span class="p">:</span>
                <span class="k">break</span>
            <span class="n">layers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
        <span class="n">inp</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">input</span>
        <span class="n">outputs_tens</span> <span class="o">=</span> <span class="p">[</span><span class="n">layer</span><span class="o">.</span><span class="n">output</span> <span class="k">for</span> <span class="n">layer</span> <span class="ow">in</span> <span class="n">layers</span><span class="p">[</span><span class="mi">1</span><span class="p">:]]</span> 
        <span class="n">functor</span> <span class="o">=</span> <span class="n">K</span><span class="o">.</span><span class="n">function</span><span class="p">([</span><span class="n">inp</span><span class="p">],</span> <span class="n">outputs_tens</span> <span class="p">)</span>
        <span class="n">outputs_tens</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">inp</span><span class="p">)</span>
        <span class="n">layer_outs</span> <span class="o">=</span> <span class="n">functor</span><span class="p">([</span><span class="n">test</span><span class="p">,</span> <span class="mf">1.</span><span class="p">])</span>
        <span class="n">layer_outs</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">test</span><span class="p">)</span>
        
        <span class="k">for</span> <span class="n">lay</span><span class="p">,</span> <span class="n">out</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">layers</span><span class="p">,</span> <span class="n">layer_outs</span><span class="p">):</span>
            <span class="n">layer_output_dict</span><span class="p">[</span><span class="n">lay</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">out</span>

        <span class="c1"># regression submodel</span>
        <span class="n">regression_submodel</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="o">-</span><span class="mi">8</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">lay</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pyramid_layers</span><span class="p">:</span>
            <span class="n">inp</span> <span class="o">=</span> <span class="n">regression_submodel</span><span class="o">.</span><span class="n">get_input_at</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">outputs_tens_</span> <span class="o">=</span> <span class="p">[</span><span class="n">lay_</span><span class="o">.</span><span class="n">output</span> <span class="k">for</span> <span class="n">lay_</span> <span class="ow">in</span> <span class="n">regression_submodel</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="mi">1</span><span class="p">:]]</span>
            <span class="n">functor_</span> <span class="o">=</span> <span class="n">K</span><span class="o">.</span><span class="n">function</span><span class="p">([</span><span class="n">inp</span><span class="p">],</span> <span class="n">outputs_tens_</span> <span class="p">)</span>
            <span class="n">test_</span> <span class="o">=</span> <span class="n">layer_output_dict</span><span class="p">[</span><span class="n">lay</span><span class="p">]</span>
            <span class="n">layer_outs_</span> <span class="o">=</span> <span class="n">functor_</span><span class="p">([</span><span class="n">test_</span><span class="p">,</span> <span class="mf">1.</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">lay_in</span><span class="p">,</span> <span class="n">lay_out</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">regression_submodel</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="n">layer_outs_</span><span class="p">):</span>
                <span class="n">layer_output_dict</span><span class="p">[</span><span class="n">lay_in</span><span class="o">.</span><span class="n">name</span><span class="o">+</span><span class="s2">&quot;_&quot;</span><span class="o">+</span><span class="n">lay</span><span class="p">]</span> <span class="o">=</span> <span class="n">lay_out</span>

        <span class="c1"># classification submodel</span>
        <span class="n">classification_submodel</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="o">-</span><span class="mi">4</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">lay</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pyramid_layers</span><span class="p">:</span>
            <span class="n">inp</span> <span class="o">=</span> <span class="n">classification_submodel</span><span class="o">.</span><span class="n">get_input_at</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">outputs_tens_</span> <span class="o">=</span> <span class="p">[</span><span class="n">lay_</span><span class="o">.</span><span class="n">output</span> <span class="k">for</span> <span class="n">lay_</span> <span class="ow">in</span> <span class="n">classification_submodel</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="mi">1</span><span class="p">:]]</span>
            <span class="n">functor_</span> <span class="o">=</span> <span class="n">K</span><span class="o">.</span><span class="n">function</span><span class="p">([</span><span class="n">inp</span><span class="p">],</span> <span class="n">outputs_tens_</span> <span class="p">)</span>
            <span class="n">test_</span> <span class="o">=</span> <span class="n">layer_output_dict</span><span class="p">[</span><span class="n">lay</span><span class="p">]</span>
            <span class="n">layer_outs_</span> <span class="o">=</span> <span class="n">functor_</span><span class="p">([</span><span class="n">test_</span><span class="p">,</span> <span class="mf">1.</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">lay_in</span><span class="p">,</span> <span class="n">lay_out</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">classification_submodel</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="n">layer_outs_</span><span class="p">):</span>
                <span class="n">layer_output_dict</span><span class="p">[</span><span class="n">lay_in</span><span class="o">.</span><span class="n">name</span><span class="o">+</span><span class="s2">&quot;_&quot;</span><span class="o">+</span><span class="n">lay</span><span class="p">]</span> <span class="o">=</span> <span class="n">lay_out</span>

        <span class="c1"># inference layers</span>
        <span class="n">inference_layers</span><span class="o">=</span> <span class="p">[</span><span class="n">lay</span> <span class="k">for</span> <span class="n">lay</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="o">-</span><span class="mi">8</span><span class="p">:]</span> <span class="k">if</span> <span class="n">lay</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">!=</span> <span class="s2">&quot;Model&quot;</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">lay</span> <span class="ow">in</span> <span class="n">inference_layers</span><span class="p">:</span>
            <span class="n">layer_name</span> <span class="o">=</span> <span class="n">lay</span><span class="o">.</span><span class="n">name</span>
            <span class="n">intermediate_layer_model</span> <span class="o">=</span> <span class="n">Model</span><span class="p">(</span><span class="n">inputs</span><span class="o">=</span><span class="n">model</span><span class="o">.</span><span class="n">input</span><span class="p">,</span><span class="n">outputs</span><span class="o">=</span><span class="n">model</span><span class="o">.</span><span class="n">get_layer</span><span class="p">(</span><span class="n">layer_name</span><span class="p">)</span><span class="o">.</span><span class="n">output</span><span class="p">)</span>
            <span class="n">layer_output_dict</span><span class="p">[</span><span class="n">layer_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">intermediate_layer_model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">test</span><span class="p">)</span>

        <span class="c1"># assign shapes</span>
        <span class="k">for</span> <span class="n">net_layer</span> <span class="ow">in</span> <span class="n">pmml_without_shape</span><span class="o">.</span><span class="n">DeepNetwork</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">NetworkLayer</span><span class="p">:</span>
            <span class="n">input_shape</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="n">net_layer</span><span class="o">.</span><span class="n">connectionLayerId</span> <span class="o">==</span> <span class="s2">&quot;na&quot;</span><span class="p">:</span>
                <span class="n">input_shape</span> <span class="o">=</span> <span class="n">output_shape</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">layer_output_dict</span><span class="p">[</span><span class="n">net_layer</span><span class="o">.</span><span class="n">layerId</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">connected_layers</span> <span class="o">=</span> <span class="n">net_layer</span><span class="o">.</span><span class="n">connectionLayerId</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;, &quot;</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">connected_layers</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="n">input_shape</span> <span class="o">=</span> <span class="p">[]</span>
                    <span class="k">for</span> <span class="n">con_lay</span> <span class="ow">in</span> <span class="n">connected_layers</span><span class="p">:</span>
                        <span class="n">input_shape</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">layer_output_dict</span><span class="p">[</span><span class="n">con_lay</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:]))</span>
                    <span class="n">input_shape</span> <span class="o">=</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">input_shape</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">input_shape</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">layer_output_dict</span><span class="p">[</span><span class="n">connected_layers</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
                <span class="k">if</span> <span class="n">net_layer</span><span class="o">.</span><span class="n">layerType</span> <span class="o">==</span> <span class="s1">&#39;FilterDetections&#39;</span><span class="p">:</span>
                    <span class="n">new_shape_lst</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">]</span>
                    <span class="k">for</span> <span class="n">o_shape</span> <span class="ow">in</span> <span class="n">layer_output_dict</span><span class="p">[</span><span class="n">net_layer</span><span class="o">.</span><span class="n">layerId</span><span class="p">]:</span>
                        <span class="n">o_shape</span> <span class="o">=</span> <span class="n">o_shape</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
                        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">o_shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                            <span class="n">shp</span> <span class="o">=</span> <span class="p">(</span><span class="n">o_shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">1</span><span class="p">)</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="n">shp</span> <span class="o">=</span> <span class="n">o_shape</span>
                        <span class="n">new_shape_lst</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">shp</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                        <span class="n">new_shape_lst</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+=</span> <span class="n">shp</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                    <span class="n">output_shape</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">new_shape_lst</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">output_shape</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">layer_output_dict</span><span class="p">[</span><span class="n">net_layer</span><span class="o">.</span><span class="n">layerId</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
            <span class="n">net_layer</span><span class="o">.</span><span class="n">LayerParameters</span><span class="o">.</span><span class="n">inputDimension</span> <span class="o">=</span> <span class="n">input_shape</span>
            <span class="n">net_layer</span><span class="o">.</span><span class="n">LayerParameters</span><span class="o">.</span><span class="n">outputDimension</span> <span class="o">=</span> <span class="n">output_shape</span>
        <span class="k">return</span> <span class="n">pmml_without_shape</span></div>

<div class="viewcode-block" id="RetinanetToPmml.get_output">    <span class="k">def</span> <span class="nf">get_output</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates Output for RetinaNet</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Nyoka&#39;s Output object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">out_flds</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">out_flds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="s2">&quot;predicted_LabelBoxScore&quot;</span><span class="p">,</span>
                <span class="n">dataType</span><span class="o">=</span><span class="s2">&quot;string&quot;</span><span class="p">,</span>
                <span class="n">feature</span><span class="o">=</span><span class="s2">&quot;predictedValue&quot;</span><span class="p">,</span>
                <span class="n">Extension</span> <span class="o">=</span> <span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">Extension</span><span class="p">(</span><span class="n">extender</span><span class="o">=</span><span class="s2">&quot;ADAPA&quot;</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;format&quot;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s2">&quot;JSON&quot;</span><span class="p">)]</span>
            <span class="p">)</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">pml</span><span class="o">.</span><span class="n">Output</span><span class="p">(</span><span class="n">OutputField</span><span class="o">=</span><span class="n">out_flds</span><span class="p">)</span></div>

<div class="viewcode-block" id="RetinanetToPmml.get_training_parameter">    <span class="k">def</span> <span class="nf">get_training_parameter</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates TrainingParameters for RetinaNet</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Nyoka&#39;s TrainingParameters object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">train_param</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">TrainingParameters</span><span class="p">(</span><span class="n">architectureName</span><span class="o">=</span><span class="s1">&#39;retinanet&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">train_param</span></div>

<div class="viewcode-block" id="RetinanetToPmml.get_local_transformation">    <span class="k">def</span> <span class="nf">get_local_transformation</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates Trasformation information for RetinaNet</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Nyoka&#39;s LocalTransformations object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">apply</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Apply</span><span class="p">(</span>
            <span class="n">function</span><span class="o">=</span><span class="s1">&#39;KerasRetinaNet:getBase64StringFromBufferedInput&#39;</span><span class="p">,</span>
            <span class="n">FieldRef</span> <span class="o">=</span> <span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">FieldRef</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">input_format</span><span class="p">)],</span>
            <span class="n">Constant</span> <span class="o">=</span> <span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">Constant</span><span class="p">(</span><span class="n">valueOf_</span><span class="o">=</span><span class="s1">&#39;tf&#39;</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">backbone_name</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;mobilenet&#39;</span><span class="p">,</span> <span class="s1">&#39;densenet&#39;</span><span class="p">]</span> <span class="k">else</span> <span class="s1">&#39;caffe&#39;</span><span class="p">)]</span>
        <span class="p">)</span>
        <span class="n">der_fld</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">DerivedField</span><span class="p">(</span>
            <span class="n">name</span><span class="o">=</span><span class="s2">&quot;base64String&quot;</span><span class="p">,</span>
            <span class="n">optype</span><span class="o">=</span><span class="s2">&quot;categorical&quot;</span><span class="p">,</span>
            <span class="n">dataType</span><span class="o">=</span><span class="s2">&quot;string&quot;</span><span class="p">,</span>
            <span class="n">Apply</span> <span class="o">=</span> <span class="n">apply</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">pml</span><span class="o">.</span><span class="n">LocalTransformations</span><span class="p">(</span><span class="n">DerivedField</span> <span class="o">=</span> <span class="p">[</span><span class="n">der_fld</span><span class="p">])</span></div>

    
<div class="viewcode-block" id="RetinanetToPmml.generate_pmml">    <span class="k">def</span> <span class="nf">generate_pmml</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">model</span><span class="p">,</span><span class="n">input_shape</span><span class="p">,</span><span class="n">input_format</span><span class="p">,</span><span class="n">trained_classes</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates PMML object for RetinaNet by combining all different part&#39;s PMML object</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        model : </span>
<span class="sd">            RetinaNet model object</span>
<span class="sd">        input_shape : tuple </span>
<span class="sd">            Shape of each training image</span>
<span class="sd">        input_format : string (optional. default=&#39;image&#39;)</span>
<span class="sd">            Input format to be used during inference with the PMML. Valid values are - </span>
<span class="sd">                &quot;image&quot; : Original image in png format</span>
<span class="sd">                &quot;encoded&quot; : Base64 encoded string of the image</span>
<span class="sd">        trained_classes : list or tuple</span>
<span class="sd">            List of the classes on which the model was trained. If not provided, default(1 to 80) classes will be used</span>
<span class="sd">            </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Generated nyoka&#39;s PMML object</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">backbone_and_anchor</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_beckbone_anchors</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">input_format</span><span class="p">,</span> <span class="n">trained_classes</span><span class="p">)</span>
        <span class="n">regression_submodel_layers</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_submodel</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="o">-</span><span class="mi">8</span><span class="p">])</span>
        <span class="n">classification_submodel_layers</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_submodel</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="o">-</span><span class="mi">4</span><span class="p">])</span>
        <span class="n">inference_layers</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_inference_layers</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
        <span class="n">backbone_and_anchor</span><span class="o">.</span><span class="n">DeepNetwork</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">NetworkLayer</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span>
            <span class="n">regression_submodel_layers</span><span class="o">+</span><span class="n">classification_submodel_layers</span><span class="o">+</span><span class="n">inference_layers</span>
        <span class="p">)</span>
        <span class="n">model_with_shape_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">assign_shapes</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">input_shape</span><span class="p">,</span> <span class="n">backbone_and_anchor</span><span class="p">)</span>
        <span class="n">model_with_shape_info</span><span class="o">.</span><span class="n">DeepNetwork</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">numberOfLayers</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">model_with_shape_info</span><span class="o">.</span><span class="n">DeepNetwork</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">NetworkLayer</span><span class="p">)</span>
        <span class="n">model_with_shape_info</span><span class="o">.</span><span class="n">DeepNetwork</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">Output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_output</span><span class="p">()</span>
        <span class="n">model_with_shape_info</span><span class="o">.</span><span class="n">DeepNetwork</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">TrainingParameters</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_training_parameter</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">input_format</span> <span class="o">==</span> <span class="s1">&#39;image&#39;</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">script_args</span><span class="p">:</span>
            <span class="n">model_with_shape_info</span><span class="o">.</span><span class="n">DeepNetwork</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">LocalTransformations</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_local_transformation</span><span class="p">()</span> 
        <span class="n">model_with_shape_info</span><span class="o">.</span><span class="n">Header</span><span class="o">.</span><span class="n">description</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">description</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pmml_obj</span> <span class="o">=</span> <span class="n">model_with_shape_info</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, maintainer@nyoka.org

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>